import { Component, OnInit, Input, OnChanges } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
import { HttpServcie } from '../../../../service/http.service';
import { Config } from '../../../../service/config';
import { HydpressHalfMonBean } from './bean/hydpressHalfMon.bean';
import { el } from "@angular/platform-browser/testing/src/browser_util";
import { NzModalService, NzModalSubject } from 'ng-zorro-antd';
import { CommFuntion } from '../../../../service/commFunction';
@Component({
  selector: 'hydpress-list',
  templateUrl: './hydpress.html',
  styleUrls: ['./hydpress.scss']
})
/**
 * 液压电梯列表
 */
export class HydpressPage implements OnInit, OnChanges {
  //获取电梯的id
  @Input() inValue: string;

  data: any;
  list: any = [{}];//返回的数据
  hydpressHalfMonBean: HydpressHalfMonBean = new HydpressHalfMonBean();
  types: any = []; //电梯类型
  config: any;
  modelTitle: string;//模态标题
  currentModal: NzModalSubject;
  isActive: number = 1;
  total: string;
  type: number;
  hydpressHalfMon: any;
  hydpressQuarter: any;
  hydpressHalfYear: any;
  hydpressYear: any;
  maintTypes = Config.MAINTTYPES;
  totalImg: any;
  constructor(
    private httpServcie: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private modalService: NzModalService,
    private commFuntion: CommFuntion
  ) {
    this.config = Config;
    this.list = [{}];
  }

  ngOnInit() {
  }

  ngOnChanges() {
    this.hydpressHalfMonBean.pageSize = 10;
    this.hydpressHalfMonBean.currentPage = 1;
    this.hydpressHalfMonBean.elevatorNum = this.inValue;
    this.query();
  }

  refreshStatus() {

  }
  displayDataChange() {

  }

  //点击头tab 切换
  doActive(ind) {
    this.isActive = ind;
  }

  toDetail(titleTpl, contentTpl, footerTpl, data) {
    //查询维保的信息
    this.httpServcie.queryHydpressDetails(data.id).subscribe(datas => {
      if (datas.status == 200) {
        this.hydpressHalfMon = datas.data.hydpress.hydpressHalfMon;
        this.hydpressQuarter = datas.data.hydpress.hydpressQuarter;//季度保
        this.hydpressHalfYear = datas.data.hydpress.hydpressHalfYear;//半年保
        this.hydpressYear = datas.data.hydpress.hydpressYear;//年保
      }
      let num = 0;
      let hydpressHalfMon = this.commFuntion.convertBean(this.hydpressHalfMon);
      num += this.commFuntion.countPicNum(hydpressHalfMon);
      if (this.hydpressQuarter) {
        let hydpressQuarter = this.commFuntion.convertBean(this.hydpressQuarter);
        num += this.commFuntion.countPicNum(hydpressQuarter);
      }
      if (this.hydpressHalfYear) {
        let hydpressHalfYear = this.commFuntion.convertBean(this.hydpressHalfYear);
        num += this.commFuntion.countPicNum(hydpressHalfYear);
      }
      if (this.hydpressYear) {
        let hydpressYear = this.commFuntion.convertBean(this.hydpressYear);
        num += this.commFuntion.countPicNum(hydpressYear);
      }
      this.totalImg = num;
    })
    this.type = data.type;
    this.modelTitle = '维保详细';
    this.currentModal = this.modalService.open({
      title: titleTpl,
      content: contentTpl,
      footer: footerTpl,
      maskClosable: false,
      width: 800,
      style: {
        top: '20px'
      },
      onOk() {
        console.log('Click ok');
      }
    });
  }

  query() {
    this.httpServcie.queryHydpress(this.hydpressHalfMonBean).subscribe(datas => {
      if (datas.status == 200) {
        this.list = datas.data.list;
        this.total = datas.data.total;
      }

    })
  }

  //分页
  nzPageSizeChange(event) {
    this.hydpressHalfMonBean.pageSize = event;
    this.hydpressHalfMonBean.currentPage = 1;
    this.query();

  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.hydpressHalfMonBean.currentPage = event;
    this.query();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.hydpressHalfMonBean.currentPage = event;
    this.query();
  }

  export(data) {
    let param = { halfMonId: data.id, elevatorNum: data.elevatorNum, elevatorType: '2' };
    this.httpServcie.exportMaint(param).subscribe(data => {
      if (data.status == 200) {
        this.download(data.data.fileUrl, data.data.fileName);
        this._notification.create('success', '提示', "导出成功");
      } else {
        this._notification.create('error', '提示', "导出失败");
      }
    })
  }
  download(url, name) {
    let link = document.createElement('a');
    let body = document.querySelector('body');

    this.getBlob(this.config.IP + this.config.API.getUpload + url).then(blob => {
      link.href = window.URL.createObjectURL(blob);
      link.download = name;
      // fix Firefox
      link.style.display = 'none';
      body.appendChild(link);
      link.click();
      body.removeChild(link);
      window.URL.revokeObjectURL(link.href);
    });
  }
  getBlob(url) {
    return new Promise(resolve => {
      const xhr = new XMLHttpRequest();
      xhr.open('GET', url, true);
      xhr.responseType = 'blob';
      xhr.onload = () => {
        if (xhr.status === 200) {
          resolve(xhr.response);
        }
      };
      xhr.send();
    });
  }
}
